package com.aijk.web.admin.h5.controller;

import com.aijk.ehm.api.EhmCompanyApi;
import com.aijk.ehm.api.EhmProblemLogApi;
import com.aijk.ehm.api.EhmProblemRecordApi;
import com.aijk.ehm.api.EhmResidentApi;
import com.aijk.ehm.model.EhmProblemLog;
import com.aijk.ehm.model.EhmProblemRecord;
import com.aijk.ehm.model.EhmResident;
import com.aijk.ehm.model.qo.EhmCompanyQo;
import com.aijk.ehm.model.qo.EhmProblemLogQo;
import com.aijk.ehm.model.qo.EhmProblemRecordQo;
import com.aijk.ehm.model.vo.EhmCompanyVo;
import com.aijk.ehm.model.vo.EhmResidentVo;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.entity.ext.QuestionExt;
import com.aijk.web.admin.entity.ext.QuestionList;
import com.aijk.web.admin.entity.ext.QuestionsExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/h5/question")
public class UserQuestionH5Controller extends BaseController {

    @Autowired
    private EhmProblemLogApi ehmProblemLogApi;

    @Autowired
    private EhmProblemRecordApi ehmProblemRecordApi;

    @Autowired
    private EhmResidentApi ehmResidentApi;

    @Autowired
    private EhmCompanyApi ehmCompanyApi;


    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    public String list_view1(Model model , Long rid , Long companyId) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        EhmProblemLogQo ehmProblemLogQo = new EhmProblemLogQo();
        ehmProblemLogQo.setRid(rid);
        ehmProblemLogQo.setCompanyId(companyId);
        List<EhmProblemLog> ehmProblemLogList =  ehmProblemLogApi.list(ehmProblemLogQo);
        EhmCompanyVo ehmCompanyVo = ehmCompanyApi.findOne(companyId);
        EhmResidentVo ehmResidentVo = ehmResidentApi.findOne(rid);
        if(ehmCompanyVo == null || ehmResidentVo == null){
            return "h5/question/question_list";
        }

        List<QuestionList> questionListList = new ArrayList<>();
        if(ehmProblemLogList == null || ehmProblemLogList.size() == 0){
            //添加可回答问卷
            QuestionList questionList = new QuestionList();
//            questionList.setCompany(ehmCompanyVo.getTitle());
            questionList.setCompanyId(ehmCompanyVo.getId());
            questionList.setRid(rid);
            questionList.setTitle("单位健康促进调查问卷");
            questionList.setUid(ehmResidentVo.getUid());
            questionListList.add(questionList);
        }
        EhmCompanyQo ehmCompanyQo = new EhmCompanyQo();
        List<EhmCompanyVo> ehmCompanyVoList = ehmCompanyApi.list(ehmCompanyQo);
        HashMap<Long , EhmCompanyVo> hashMap = new HashMap();
        for (EhmCompanyVo vo : ehmCompanyVoList){
            hashMap.put(vo.getId() , vo);
        }

        ehmProblemLogQo.setCompanyId(null);
        List<EhmProblemLog> ehmProblemLogList1 = ehmProblemLogApi.list(ehmProblemLogQo);
        if(null!=ehmProblemLogList1 && ehmProblemLogList1.size() > 0){
            for (EhmProblemLog log : ehmProblemLogList1){
                QuestionList questionList = new QuestionList();
                if(hashMap.containsKey(log.getCompanyId())){
                    questionList.setUid(log.getUid());
                    questionList.setRid(log.getRid());
                    questionList.setCompanyId(log.getCompanyId());
                    questionList.setCompany(hashMap.get(log.getCompanyId()).getTitle());
                    questionList.setTitle(log.getProblemTitle());
                    questionList.setDate(simpleDateFormat.format(log.getCreated()));
                    questionListList.add(questionList);
                }
            }
        }
        model.addAttribute("questionList",questionListList);
        return "h5/question/question_list";
    }

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String list_view(Model model , Long rid , Long companyId) {
        EhmProblemLogQo ehmProblemLogQo = new EhmProblemLogQo();
        ehmProblemLogQo.setRid(rid);
        ehmProblemLogQo.setCompanyId(companyId);
        List<EhmProblemLog> ehmProblemLogList =  ehmProblemLogApi.list(ehmProblemLogQo);
        if(ehmProblemLogList != null && ehmProblemLogList.size() > 0){
            EhmProblemRecordQo ehmProblemRecordQo = new EhmProblemRecordQo();
            ehmProblemRecordQo.setRid(ehmProblemLogList.get(0).getRid());
            ehmProblemRecordQo.setCompanyId(ehmProblemLogList.get(0).getCompanyId());
            List<EhmProblemRecord> ehmProblemRecordList = ehmProblemRecordApi.list(ehmProblemRecordQo);
            QuestionExt ext = converModel(ehmProblemRecordList);
            List<String> stringList = new ArrayList<>();
            if(ext!=null&&ext.getQx()!=null  ){
                model.addAttribute("list1" , ext.getQx());
            }else{
                model.addAttribute("list1" , stringList);
            }
            if(ext!=null&&ext.getJkcj()!=null  ){
                model.addAttribute("list2" , ext.getJkcj());
            }else{
                model.addAttribute("list2" , stringList);
            }
            if(ext!=null&&ext.getLjzs()!=null  ){
                model.addAttribute("list3" , ext.getLjzs());
            }else {
                model.addAttribute("list3" , stringList);
            }
            model.addAttribute("rid" , rid);
            model.addAttribute("question" , converModel(ehmProblemRecordList));
            return "h5/question/questions_no";
        }else{
            model.addAttribute("rid" , rid);
            model.addAttribute("companyId" , companyId);
            return "h5/question/questions";
        }
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg save(QuestionsExt questionsExt,Long id) {
        EhmResident ehmResident = ehmResidentApi.findOne(id);
        Long companyId = ehmResident.getCompanyId();
        Long uid = ehmResident.getUid();
        //问卷日志
        EhmProblemLog ehmProblemLog = new EhmProblemLog();
        ehmProblemLog.setRid(id);
        ehmProblemLog.setCompanyId(companyId);
        ehmProblemLog.setUid(uid);
        ehmProblemLog.setCreated(new Date());
        ehmProblemLog.setPid(1l);
        ehmProblemLog.setProblemTitle("单位健康促进调查问卷");
        Long pid = ehmProblemLogApi.save(ehmProblemLog);

        //问卷记录
        //1性别
        EhmProblemRecord ehmProblemRecord = new EhmProblemRecord();
        ehmProblemRecord.setCompanyId(companyId);
        ehmProblemRecord.setRid(id);
        ehmProblemRecord.setUid(uid);
        ehmProblemRecord.setContent(String.valueOf(questionsExt.getSex()));
        ehmProblemRecord.setSortId(1);
        ehmProblemRecord.setProblem("性别");
        ehmProblemRecord.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord);

        //2年龄
        EhmProblemRecord ehmProblemRecord2 = new EhmProblemRecord();
        ehmProblemRecord2.setCompanyId(companyId);
        ehmProblemRecord2.setRid(id);
        ehmProblemRecord2.setUid(uid);
        ehmProblemRecord2.setContent(String.valueOf(questionsExt.getAge()));
        ehmProblemRecord2.setSortId(2);
        ehmProblemRecord2.setProblem("年龄");
        ehmProblemRecord2.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord2);

        //3身高: 厘米
        EhmProblemRecord ehmProblemRecord3 = new EhmProblemRecord();
        ehmProblemRecord3.setCompanyId(companyId);
        ehmProblemRecord3.setRid(id);
        ehmProblemRecord3.setUid(uid);
        ehmProblemRecord3.setContent(questionsExt.getHeight());
        ehmProblemRecord3.setSortId(3);
        ehmProblemRecord3.setProblem("3身高: 厘米");
        ehmProblemRecord3.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord3);

        //4体重: 公斤
        EhmProblemRecord ehmProblemRecord4 = new EhmProblemRecord();
        ehmProblemRecord4.setCompanyId(companyId);
        ehmProblemRecord4.setRid(id);
        ehmProblemRecord4.setUid(uid);
        ehmProblemRecord4.setContent(questionsExt.getWeight());
        ehmProblemRecord4.setSortId(4);
        ehmProblemRecord4.setProblem("4体重: 公斤");
        ehmProblemRecord4.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord4);

        //5请在下列符合您现状的选项上打勾:（可多选）
        EhmProblemRecord ehmProblemRecord5 = new EhmProblemRecord();
        ehmProblemRecord5.setCompanyId(companyId);
        ehmProblemRecord5.setRid(id);
        ehmProblemRecord5.setUid(uid);
        ehmProblemRecord5.setContent(questionsExt.getQx());
        ehmProblemRecord5.setSortId(5);
        ehmProblemRecord5.setProblem("5请在下列符合您现状的选项上打勾:（可多选）");
        ehmProblemRecord5.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord5);

        //6您经常参加体育锻炼吗
        EhmProblemRecord ehmProblemRecord6 = new EhmProblemRecord();
        ehmProblemRecord6.setCompanyId(companyId);
        ehmProblemRecord6.setRid(id);
        ehmProblemRecord6.setUid(uid);
        ehmProblemRecord6.setContent(String.valueOf(questionsExt.getDl()));
        ehmProblemRecord6.setSortId(6);
        ehmProblemRecord6.setProblem("6您经常参加体育锻炼吗");
        ehmProblemRecord6.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord6);

        //7您认为您目前的身体状况是
        EhmProblemRecord ehmProblemRecord7 = new EhmProblemRecord();
        ehmProblemRecord7.setCompanyId(companyId);
        ehmProblemRecord7.setRid(id);
        ehmProblemRecord7.setUid(uid);
        ehmProblemRecord7.setContent(String.valueOf(questionsExt.getStzk()));
        ehmProblemRecord7.setSortId(7);
        ehmProblemRecord7.setProblem("7您认为您目前的身体状况是");
        ehmProblemRecord7.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord7);

        //8您喜欢的健康促进方式有:（可多选）
        EhmProblemRecord ehmProblemRecord8 = new EhmProblemRecord();
        ehmProblemRecord8.setCompanyId(companyId);
        ehmProblemRecord8.setRid(id);
        ehmProblemRecord8.setUid(uid);
        ehmProblemRecord8.setContent(questionsExt.getJkcj());
        ehmProblemRecord8.setSortId(8);
        ehmProblemRecord8.setProblem("8您喜欢的健康促进方式有:（可多选）");
        ehmProblemRecord8.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord8);

        //9您最希望了解哪些知识 ?（可多选）
        EhmProblemRecord ehmProblemRecord9 = new EhmProblemRecord();
        ehmProblemRecord9.setCompanyId(companyId);
        ehmProblemRecord9.setRid(id);
        ehmProblemRecord9.setUid(uid);
        ehmProblemRecord9.setContent(questionsExt.getLjzs());
        ehmProblemRecord9.setSortId(9);
        ehmProblemRecord9.setProblem("9您最希望了解哪些知识 ?（可多选）");
        ehmProblemRecord9.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecord9);

        //10您希望多久开展一次健康促进活动
        EhmProblemRecord ehmProblemRecorda = new EhmProblemRecord();
        ehmProblemRecorda.setCompanyId(companyId);
        ehmProblemRecorda.setRid(id);
        ehmProblemRecorda.setUid(uid);
        ehmProblemRecorda.setContent(String.valueOf(questionsExt.getJkzjhd()));
        ehmProblemRecorda.setSortId(10);
        ehmProblemRecorda.setProblem("10您希望多久开展一次健康促进活动");
        ehmProblemRecorda.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecorda);

        //11您对单位现阶段开展的健康促进活动是否满意
        EhmProblemRecord ehmProblemRecordb = new EhmProblemRecord();
        ehmProblemRecordb.setCompanyId(companyId);
        ehmProblemRecordb.setRid(id);
        ehmProblemRecordb.setUid(uid);
        ehmProblemRecordb.setContent(String.valueOf(questionsExt.getSfmy()));
        ehmProblemRecordb.setSortId(11);
        ehmProblemRecordb.setProblem("11您对单位现阶段开展的健康促进活动是否满意");
        ehmProblemRecordb.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecordb);

        //12您对单位健康促进活动有什么意见和建议
        EhmProblemRecord ehmProblemRecordc = new EhmProblemRecord();
        ehmProblemRecordc.setCompanyId(companyId);
        ehmProblemRecordc.setRid(id);
        ehmProblemRecordc.setUid(uid);
        ehmProblemRecordc.setContent(questionsExt.getYj());
        ehmProblemRecordc.setSortId(12);
        ehmProblemRecordc.setProblem("12您对单位健康促进活动有什么意见和建议");
        ehmProblemRecordc.setPid(pid);
        ehmProblemRecordApi.save(ehmProblemRecordc);

        return RetMsg.success();
    }


    private QuestionExt converModel(List<EhmProblemRecord> ehmProblemRecordList){
        QuestionExt ext = new QuestionExt();
        if(null!=ehmProblemRecordList && ehmProblemRecordList.size()>0){

            List<String> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            List<String> list3 = new ArrayList<>();
            for (EhmProblemRecord record : ehmProblemRecordList){
                if(record.getSortId() == 1){
                    ext.setSex(com.aijk.commons.utils.Utils.toInteger(record.getContent()));
                }

                if(record.getSortId() == 2){
                    ext.setAge(com.aijk.commons.utils.Utils.toInteger(record.getContent()));
                }

                if(record.getSortId() == 3){
                    ext.setHeight(record.getContent());
                }

                if(record.getSortId() == 4){
                    ext.setWeight(record.getContent());
                }

                if(record.getSortId() == 5){
                    list1.add(record.getContent());
                }

                if(record.getSortId() == 6){
                    ext.setDl(com.aijk.commons.utils.Utils.toInteger(record.getContent()));
                }

                if(record.getSortId() == 7){
                    ext.setStzk(com.aijk.commons.utils.Utils.toInteger(record.getContent()));
                }

                if(record.getSortId() == 8){
                    list2.add(record.getContent());
                }

                if(record.getSortId() == 9){
                    list3.add(record.getContent());
                }

                if(record.getSortId() == 10){
                    ext.setJkzjhd(com.aijk.commons.utils.Utils.toInteger(record.getContent()));
                }

                if(record.getSortId() == 11){
                    ext.setSfmy(com.aijk.commons.utils.Utils.toInteger(record.getContent()));
                }

                if(record.getSortId() == 12){
                    ext.setYj(record.getContent());
                }
            }
            ext.setQx(list1);
            ext.setJkcj(list2);
            ext.setLjzs(list3);
        }

        return ext;
    }

}
